package edit;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import pulpcore.sprite.Sprite;
import pulpcore.Input;

import java.util.List;
import java.util.ArrayList;
import java.awt.geom.Ellipse2D;
import java.awt.*;

import draw.DrawBoxSprite;
import draw.DrawShapeSprite;
import pulpfizz.physics.BodyUtils;
import pulpfizz.physics.Material;
import pulpfizz.physics.PolygonUtils;
import pulpfizz.pulp.body.Java2DBodySprite;
import pulpfizz.pulp.body.ShapeBodyDrawer;
import pulpfizz.pulp.body.BodyDrawer;
import goliath.BasicDemo;

/**
 * @author will
 * @date Aug 10, 2008 2:57:49 PM
 */
public abstract class DrawShapeContext extends EditContext {

    public Vec2 startPos;
    Sprite drawSprite;
    public Shape shape;


    public DrawShapeContext() {
        super();
        startPos = new Vec2();
        shape = createShape();
    }

    boolean pressed;

    public void update(int elapsedTime) {
        setShape(startPos,mousePos);

        if (Input.isMousePressed()) {
            pressed = true;
            // Record the starting position
            startPos.set(mousePos);

            drawSprite = new DrawShapeSprite(this);
            layer.add(drawSprite);
        }

        if (Input.isMouseReleased() && pressed) {
            pressed = false;
            layer.remove(drawSprite);
            try {
                Body myBox = PolygonUtils.areaToBody(layer, shape, 0.1f);
                if (myBox != null) {
                    if (!BasicDemo.isFixed()) {
                        Material.DEAD_WEIGHT().applyAll(myBox);
                    }
//            Java2DBodySprite bs = new Java2DBodySprite(myBox,layer,5, BasicDemo.getDrawColor(), BasicDemo.getFillColor());
                    ShapeBodyDrawer bd = new ShapeBodyDrawer(shape);
                    bd.setStrokePaint(BasicDemo.getDrawColor());
                    bd.setPaint(BasicDemo.getFillColor());
                    Java2DBodySprite bs = new Java2DBodySprite(myBox, layer, 5, bd);

                    layer.add(bs);
                }
            } catch (Exception e) {

            }
        }
    }

    public abstract Shape createShape();
    public abstract Shape setShape(Vec2 start, Vec2 now);

    public void reset() {
        startPos.setZero();
        pressed = false;
    }

    public String toString() {
        return "Shape";
    }


}
